﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Medianamik.Core;
using Medianamik.Core.Action;
using Medianamik.Core.Lock;

namespace Medianamik.UI.Lock
{
    public class ReleaseLockAction : IActionHandler
    {
       
        #region IActionHandler Members

        public void Initialize(ActionManager events)
        {
            events.AfterUpdateNode += events_AfterUpdateNode;
            events.AfterRevertNodeInstance += events_AfterRevertNodeInstance;
        }
        public string Description
        {
            get
            {
                return "Libère les verrous lors de l'enregistrement d'un Noeud";
            }
        }

        #endregion

        void events_AfterUpdateNode(ActionEventArgs<Node> args)
        {
            if (args.Item != null
                && LockManager.IsLocked(args.Item.ID))
            {
                LockManager.UnlockObject(args.Item.ID);
            }
        }

        void events_AfterRevertNodeInstance(ActionEventArgs<NodeInstance> args)
        {
            if (args.Item != null
                 && LockManager.IsLocked(args.Item.Node.ID))
            {
                LockManager.UnlockObject(args.Item.Node.ID);
            }
        }
    }
}
